package com.project.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.project.model.ShippingAddressModel;
import com.project.service.ShippingAddressService;

@RestController
@RequestMapping(value = "myAddress")
public class ShippingAddressController {
	
	@Autowired
	private ShippingAddressService shippingAddressService;
	
	@RequestMapping("info")
	public ModelAndView info(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		String viewName = "/again_login.jsp";
		
		HttpSession session = request.getSession();
		if (null != session.getAttribute("userId")) {
			List<ShippingAddressModel> listShippingAddress = shippingAddressService.getShippingAddressModelByMemberId(Integer.parseInt(session.getAttribute("userId").toString()));
			if (null == listShippingAddress || listShippingAddress.size() == 0) {
				listShippingAddress = new ArrayList<ShippingAddressModel>();
			}
			mav.addObject("shippingAddres", listShippingAddress.get(0));
			viewName = "../new_address.jsp";
		} else {
			mav.addObject("page", "/myAddress/info");
			// request.getSession().setAttribute("loginPage", "/myAddress/info");
		}
		
		mav.setViewName(viewName);
		return mav;
	}
	
	@RequestMapping("add")
	public @ResponseBody Map<String, String> add(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		HttpSession session = request.getSession();
		if (null == session.getAttribute("userId")) {
			map.put("code", "-1");
			map.put("msg", "登录已失效，请重新登录");
			return map;
		}
		int userId = (int)session.getAttribute("userId");
		String code = request.getParameter("code");
		
		String name = request.getParameter("name");
		if (null == name || "".equals(name.trim())) {
			map.put("code", "0");
			map.put("msg", "性名不能为空");
			return map;
		}
		String phone = request.getParameter("phone");
		if (null == phone || "".equals(phone.trim())) {
			map.put("code", "0");
			map.put("msg", "电话不能为空");
			return map;
		}
		String province = request.getParameter("province");
		if (null == province || "".equals(province.trim()) || "000000".equals(province)) {
			map.put("code", "0");
			map.put("msg", "请选择所属省");
			return map;
		}
		String city = request.getParameter("city");
		if (null == city || "".equals(city.trim()) || "000000".equals(city)) {
			map.put("code", "0");
			map.put("msg", "请选择所属市");
			return map;
		}
		String area = request.getParameter("area");
		if (null == area || "".equals(area.trim()) || "000000".equals(area)) {
			map.put("code", "0");
			map.put("msg", "请选择所属县");
			return map;
		}
		
		String address = request.getParameter("address");
		if (null == address || "".equals(address.trim())) {
			map.put("code", "0");
			map.put("msg", "地址不能为空");
			return map;
		}
		
		ShippingAddressModel shippingAddressModel = new ShippingAddressModel();
		shippingAddressModel.setMember_id(userId);
		shippingAddressModel.setName(name);
		shippingAddressModel.setPhone(phone);
		shippingAddressModel.setCode(code);
		shippingAddressModel.setProvince(province);
		shippingAddressModel.setCity(city);
		shippingAddressModel.setArea(area);
		shippingAddressModel.setAddress(address);
		int ret = shippingAddressService.insert(shippingAddressModel);
		if (ret > 0) {
			map.put("code", "1");
			map.put("msg", "添加收货成功");
		} else {
			map.put("code", "0");
			map.put("msg", "添加收货失败");
		}
		return map;
	}
	
	@RequestMapping("edit")
	public @ResponseBody Map<String, String> edit(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		HttpSession session = request.getSession();
		if (null == session.getAttribute("userId")) {
			map.put("code", "-1");
			map.put("msg", "登录已失效，请重新登录");
			return map;
		}
		int userId = (int)session.getAttribute("userId");
		String code = request.getParameter("code");
		int id = Integer.parseInt(request.getParameter("id").toString());
		
		String name = request.getParameter("name");
		if (null == name || "".equals(name.trim())) {
			map.put("code", "0");
			map.put("msg", "性名不能为空");
			return map;
		}
		String phone = request.getParameter("phone");
		if (null == phone || "".equals(phone.trim())) {
			map.put("code", "0");
			map.put("msg", "电话不能为空");
			return map;
		}
		String province = request.getParameter("province");
		if (null == province || "".equals(province.trim()) || "000000".equals(province)) {
			map.put("code", "0");
			map.put("msg", "请选择所属省");
			return map;
		}
		String city = request.getParameter("city");
		if (null == city || "".equals(city.trim()) || "000000".equals(city)) {
			map.put("code", "0");
			map.put("msg", "请选择所属市");
			return map;
		}
		String area = request.getParameter("area");
		if (null == area || "".equals(area.trim()) || "000000".equals(area)) {
			map.put("code", "0");
			map.put("msg", "请选择所属县");
			return map;
		}
		
		String address = request.getParameter("address");
		if (null == address || "".equals(address.trim())) {
			map.put("code", "0");
			map.put("msg", "地址不能为空");
			return map;
		}
		
		ShippingAddressModel shippingAddressModel = new ShippingAddressModel();
		shippingAddressModel.setId(id);
		shippingAddressModel.setMember_id(userId);
		shippingAddressModel.setName(name);
		shippingAddressModel.setPhone(phone);
		shippingAddressModel.setCode(code);
		shippingAddressModel.setProvince(province);
		shippingAddressModel.setCity(city);
		shippingAddressModel.setArea(area);
		shippingAddressModel.setAddress(address);
		int ret = shippingAddressService.update(shippingAddressModel);
		if (ret > 0) {
			map.put("code", "1");
			map.put("msg", "添加收货成功");
		} else {
			map.put("code", "0");
			map.put("msg", "添加收货失败");
		}
		return map;
	}
}